﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using Eudaemon.ProeAssist.WinGUIControls.Properties;
using System.Drawing;

namespace Eudaemon.ProeAssist.WinGUIControls
{
    public class DriveList : System.Windows.Forms.ToolStrip, ISupportInitialize
    {
        public DriveList()
        {
        }

        #region ISupportInitialize Members

        public void BeginInit()
        {
            InitAppearance();
            LoadItems();
        }

        public void EndInit()
        {
        }

        #endregion

        public void RefreshList()
        {
            LoadItems();
        }

        public void SetListChecked(string path)
        {
            string currentPathRoot = Directory.GetDirectoryRoot(path);
            ToolStripButton btn;
            foreach (ToolStripItem item in this.Items)
            {
                if (item is ToolStripButton)
                {
                    btn = (ToolStripButton)item;
                    if (path == btn.ToolTipText || currentPathRoot == btn.ToolTipText)
                    {
                        this.ListBtn_ClickChecked(btn, null);
                        break;
                    }
                }
            }
        }

        private void InitAppearance()
        {
            this.GripStyle = ToolStripGripStyle.Hidden;
            this.LayoutStyle = ToolStripLayoutStyle.VerticalStackWithOverflow;
            this.ImageScalingSize = new System.Drawing.Size(16, 16);
        }

        private void LoadItems()
        {
            ClearCurrentItems();
            LoadSpecifiedSystemFolders();
            LoadDrives();
        }

        private void LoadDrives()
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            string driveDisplayLabel;
            Bitmap bmpFixedDrive =Resources.drive;
            Bitmap bmpCDRom = Resources.cd;
            Bitmap bmpRemovable = Resources.removable;       
            Bitmap bmpNetwork = Resources.drive_network;
            Image img;
            foreach (DriveInfo drive in allDrives)
            {
                if (drive.IsReady)
                {
                    driveDisplayLabel = drive.Name;
                    if(drive.VolumeLabel.Length > 0)
                        driveDisplayLabel =  drive.VolumeLabel+ " (" + driveDisplayLabel + ")" ;
                    switch (drive.DriveType)
                    {
                        case DriveType.Fixed:
                            img = (Image)bmpFixedDrive;
                            break;
                        case DriveType.CDRom:
                            img = (Image)bmpCDRom;
                            break;
                        case DriveType.Removable:
                            img = (Image)bmpRemovable;
                            break;
                        case DriveType.Network:
                            img = (Image)bmpNetwork;
                            break;
                        default:
                            img = (Image)bmpFixedDrive;
                            break;
                    }
                    AddItem(driveDisplayLabel, img, drive.Name);
                }
            }                        
        }

        private void LoadSpecifiedSystemFolders()
        {
            // "Desktop" Folder
            Bitmap bmp = Resources.Desktop;
            Image img = (Image)bmp;
            string pathDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            AddItem("Desktop", img, pathDesktop);
            // "My Documents" Folder
            bmp = Resources.MyDoc;
            img = (Image)bmp;
            string pathMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            AddItem("My Documents", img, pathMyDocuments);
        }

        private void AddItem(string label, Image img, string folderPath)
        {
            ToolStripButton btn = new ToolStripButton(label, img); 
            btn.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            btn.ImageAlign = ContentAlignment.MiddleLeft;
            btn.TextAlign = ContentAlignment.MiddleLeft;
            btn.CheckOnClick = true;
            btn.AutoToolTip = false;
            // Trick: let the ToolTipText property store the path string
            btn.ToolTipText = folderPath;
            btn.Click += new EventHandler(ListBtn_ClickChecked);
            this.Items.Add(btn);
        }

        /// <summary>
        /// Only one button will be selected at the same time.
        /// </summary>
        /// <param name="sender">sender should be ToolStripButton</param>
        /// <param name="e"></param>
        private void ListBtn_ClickChecked(object sender, EventArgs e)
        {
            ToolStripButton btn;
            foreach (ToolStripItem item in this.Items)
            {
                if (item is ToolStripButton)
                {
                    btn = (ToolStripButton)item;
                    btn.Checked = false;
                }
            }
            if (sender is ToolStripButton)
            {
                btn = (ToolStripButton)sender;
                btn.Checked = true;
            }
        }

        private void ClearCurrentItems()
        {
            this.Items.Clear();
        }

    }
}
